/** ------------------------------------------------------------------------
 *	VisualGroup JavaScript prototype Classes, version draf
 *  (c) 2006 Bill Vison <vison_ruan@126.com>
 *
 *
/*--------------------------------------------------------------------------*/
using("Vsg.Common.Logger");
using("Vsg.Http.HttpAction");
using("Vsg.Validator.ValidatorMethods");
using("Vsg.Validator.ValidatorTransaction");

Vsg.Event.CommonServiceEvent = Class.create();
Vsg.Event.CommonServiceEvent.prototype = {
	initialize:function() {
		
	}
}

// ------------------------- set the common event --------------------------
Vsg.Event.CommonServiceEvent.createView = function($action) {
	var ah = new Vsg.Http.HttpAction();
	ah.setHandlerType(Vsg.Http.HttpAction.HT_GOTO);
	ah.setAction($action);
		
	try {
		ah.execute();
	} catch (e) {
		Vsg.Common.Logger.warn(e);
	}
}

// edit view
Vsg.Event.CommonServiceEvent.editView = function($action , $ids , $paramsName) {
	try {
		if ($paramsName == undefined || $paramsName == "") {
			throw "The parameter name for action is undefined! Please reset it!";
		}
	} catch (e) {
		alert(e);
		return ;
	}
	
	var $vali;
	var $trans;
	try {
		$vali = new Vsg.Validator.ValidatorMethods();
		$trans = new Vsg.Validator.ValidatorTransaction();
	} catch (ex) {
		Vsg.Common.Logger.warn(ex);
	}

	/**
	 * check the value
	 */
	$trans.addCheck($vali.selected,$ids);
	$trans.addCheck($vali.selectOnly,$ids);

	// set the transation 
	try {
		var $selectedValue = null;
		var result = $trans.getResult();
		if (result) {
			var checkBoxes = document.getElementsByName($ids);
			for (var i = 0 ; i < checkBoxes.length ;i++) {
				checkBox = checkBoxes[i];
				if (checkBox.checked) {
					$selectedValue = checkBox.value;
					break;
				}
			}
		}
		
		// set the info tothe log
		Vsg.Common.Logger.trace("[Vsg.Event.CommonServiceEvent.editView()] : select checkbox value '" + $selectedValue+"'");	
			
		if (result) {
			var $ah = new Vsg.Http.HttpAction();
			$ah.setHandlerType(Vsg.Http.HttpAction.HT_GOTO);
			$ah.setAction($action);
			$ah.addParameter($paramsName,$selectedValue);
			$ah.execute();				
		}
		
	} catch (ex) {
		Vsg.Common.Logger.warn(ex);
	}
}

// remove action
Vsg.Event.CommonServiceEvent.removeAction = function($action , $ids , $paramsName) {

	try {
		if ($paramsName == undefined || $paramsName == "") {
			throw "The parameter name for action is undefined! Please reset it!";
		}
	} catch (e) {
		alert(e);
		return ;
	}

	var $vali = new Vsg.Validator.ValidatorMethods();
	var $trans = new Vsg.Validator.ValidatorTransaction();
		
	/**
	 * check the value
	 */
	$trans.addCheck($vali.selected,$ids);

	// set the transation 
	try {

		var $selValues = new Array();
		var $result = $trans.getResult();
		if ($result) {
			var $checkBoxes = document.getElementsByName($ids);
			var $checkBox;
			var $value = 0;
			for (var i = 0 ; i < $checkBoxes.length ;i++) {
				$checkBox = $checkBoxes[i];
				if ($checkBox.checked) {
					$value = $checkBox.value;
					$selValues.push($value);					
				}
			}
		}
			
		// set the info tothe log
		Vsg.Common.Logger.trace("[Vsg.Http.RequestHandler.View.edit()] : select checkbox value '" + $selValues+"'");
		
		if ($result) {
			var $ah = new Vsg.Http.HttpAction();
			$ah.setHandlerType(Vsg.Http.HttpAction.HT_GOTO);
			$ah.setAction($action);
			for (var $i = 0 ; $i < $selValues.length ; $i++)  {
				$ah.addParameter($paramsName,$selValues[$i]);
			}
			$ah.execute();
		}
			
		
	} catch (ex) {
		Vsg.Common.Logger.warn(ex);
		return ;
	}

}

